package com.oasystem.web.controller.system;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oasystem.domain.PagingBean;
import com.oasystem.domain.user.Permission;
import com.oasystem.domain.user.User;
import com.oasystem.exceptions.ClassifyAlreadyExistException;
import com.oasystem.service.system.SystemService;

@Controller
@RequestMapping(value = "/MainMenu/system")
public class SystemController {
	@Resource(name = "systemService")
	private SystemService systemService;

	@RequestMapping(value = "/userManagement")
	public String userManagement(Model model) {
		model.addAttribute("mainMenu", "system");
		model.addAttribute("subMenu", "user");
		return "system.user";
	}

	@RequestMapping(value = "/userList")
	public String userList(Model model, String pageIndex) {
		PagingBean<User> pagingBean = new PagingBean<User>();
		pagingBean.setCurrentPage(Integer.valueOf(pageIndex));
		try {
			systemService.userList(pagingBean);
			model.addAttribute("pagingBean", pagingBean);
		} catch (Exception e) {
			model.addAttribute("error", e.getMessage());
		}
		return "system.userTable";
	}

	@RequestMapping(value = "/saveUser")
	public @ResponseBody
	ModelMap saveUser(String name,String loginName, String password, String permission) {
		ModelMap map = new ModelMap();
		User user = new User();
		user.setName(name);
		user.setLoginName(loginName);
		user.setPassword(password);
		Permission perm = new Permission();
		perm.setCode(permission);
		user.setPermission(perm);
		try {
			systemService.saveUser(user);
		} catch (Exception e) {
			map.addAttribute("error", e.getMessage());
		}
		return map;
	}

	@RequestMapping(value = "/loadUser")
	public @ResponseBody
	ModelMap loadUser(String userid) {
		ModelMap map = new ModelMap();
		try {
			map.addAttribute("user", systemService.loadUser(userid));
		} catch (Exception e) {
			map.addAttribute("error", e.getMessage());
		}
		return map;
	}
	
	@RequestMapping(value = "/updateUser")
	public @ResponseBody
	ModelMap updateUser(String userId,String loginName, String newName, String newPassword, String newPermission) {
		ModelMap map = new ModelMap();
		User user = new User();
		user.setId(userId);
		user.setLoginName(loginName);
		user.setName(newName);
		user.setPassword(newPassword);
		Permission perm = new Permission();
		perm.setCode(newPermission);
		user.setPermission(perm);
		try {
			systemService.updateUser(user);
		} catch (Exception e) {
			map.addAttribute("error", e.getMessage());
		}
		return map;
	}

	@RequestMapping(value = "/deleteUser")
	public @ResponseBody
	ModelMap deleteUser(String userid) {
		ModelMap map = new ModelMap();
		try {
			systemService.deleteUser(userid);
		} catch (Exception e) {
			map.addAttribute("error", e.getMessage());
		}
		return map;
	}
	
	@RequestMapping(value = "/fundClassifyManagement")
	public String fundClassifyManagement(Model model) {
		model.addAttribute("mainMenu", "system");
		model.addAttribute("subMenu", "fundType");
		return "system.fundType";
	}

	@RequestMapping(value = "/fundClassifyTable")
	public String fundClassifyTable(Model model) {
		try {
			model.addAttribute("classifyList", systemService.fundClassifyList());
		} catch (Exception e) {
			model.addAttribute("error", e.getMessage());
		}
		return "system.fundClassifyTable";
	}

	@RequestMapping(value = "/saveFundClassify")
	public @ResponseBody
	ModelMap saveFundClassify(Model model, String newClassify) {
		ModelMap map = new ModelMap();
		try {
			systemService.saveClassify(newClassify);
		} catch (ClassifyAlreadyExistException e) {
			map.addAttribute("error", e.getMessage());
		}
		return map;
	}

	@RequestMapping(value = "/deleteFundClassify")
	public @ResponseBody
	ModelMap deleteFundClassify(Model model, String classifyId) {
		ModelMap map = new ModelMap();
		try {
			systemService.deleteClassify(classifyId);
		} catch (Exception e) {
			map.addAttribute("error", "删除失败");
		}
		return map;
	}

	@RequestMapping(value = "/updateFundClassify")
	public @ResponseBody
	ModelMap updateFundClassify(Model model, String classifyId,
			String newClassify) {
		ModelMap map = new ModelMap();
		try {
			systemService.updateClassify(classifyId, newClassify);
		} catch (Exception e) {
			map.addAttribute("error", "修改失败");
		}
		return map;
	}

}
